
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.PrintStream;
import java.awt.FontMetrics;
import java.util.ArrayList;

public class Button extends _DesigntElement
{
    private Action action;
    private Vec2 length;
    protected Color myColor;
    protected Color MouseOnShade;
    protected Color Clicked;
    protected Color Border;
    
    protected boolean mouseOn = false;
    protected boolean clicking = false;
    public boolean selected = false; 
    public boolean drawGreen = false;
    
    
    
    
    int width =200, height=180;
    boolean inLinesBroken = false;
    ArrayList<String> textes = null;
    int schriftOffset = 14;
    int zeilenabstand = 2;
    Font backUpFont;
    private String text= "";
    Button( Action a, Vec2 pos, Vec2 length, Color c, String text){
        super(pos.X, pos.Y);
        action = a;
        this.length = length;
        this.text = text;
        myColor = c;
        float mouseOnShadeScale = 0.9f;
        float clickScale = 0.7f;
        float border = 1.5f;
        MouseOnShade = new Color((int)(c.getRed() * mouseOnShadeScale),
        (int)(c.getGreen() * mouseOnShadeScale),(int)(c.getBlue() * mouseOnShadeScale));
        Clicked = new Color((int)(c.getRed() * clickScale),(int)(c.getGreen() * clickScale),(int)(c.getBlue() * clickScale));
         
        Border = Color.WHITE;
    }
    public void switchSelect(){
        selected = !selected;
    }
    public void setLength(Vec2 length){
        if(length != null)
        this.length = length;
    }
    public void setAction(Action act){
        action = act;
    }
    public void setText(String s){
        text = s;
        inLinesBroken = false;
    }
    public String getText(){
       return text;
    }
    public void mouseOn(Vec2 mPos, boolean Click){
        int x = Phy.getPosition().X;
        int y = Phy.getPosition().Y;
        mouseOn = false;
        clicking = false;
        if(mPos.X > x && mPos.X< x+length.x)
        {
            if(mPos.Y > y && mPos.Y<y +length.y)
            {
                if(Click)
                {
                    if(!clicking)
                    {
                        action.act();
                    }
                    clicking = true;
                    
                }
                mouseOn = true;
            }
        }
        
    }
    public  void paint(Graphics g){
        Color currentColor= null;
        if(clicking)
        {
            currentColor = Clicked;
        }
        else if(mouseOn){
            currentColor = MouseOnShade;
        }
        else 
            currentColor = myColor;
        
        int x = Phy.getPosition().X;
        int y = Phy.getPosition().Y;
        
        g.setColor(currentColor);
        g.fillRoundRect(
        x, y, length.X, length.Y,
                 20,20);
        if(selected || drawGreen){
            g.setColor(Border);
            if(drawGreen)
                g.setColor(Color.GREEN);
            g.drawRoundRect(
            x, y, length.X, length.Y,
                 20,20);
            g.drawRoundRect(
            x+1, y+1, length.X-2, length.Y-2,
                 20,20);
        }
        int stiftx =0, stifty =0;
        stiftx = (int)Phy.getPosition().x;
        stifty = (int)Phy.getPosition().y;
        //g.drawImage(img,
        //    stiftx, stifty, width, height,
        //    null);
        
        Color c = new Color(255 - currentColor.getRed(),
        255 - currentColor.getGreen(),
        255 - currentColor.getBlue());

        g.setColor(c);
        Font f = g.getFont();
        while(!inLinesBroken)
        {
            breakLongStringInLines(g.getFontMetrics());
            if(!inLinesBroken){
                g.setFont(g.getFont().deriveFont(g.getFont().getSize()-1f));
                backUpFont = g.getFont();
            }
        }
        if(backUpFont!= null)
            g.setFont(backUpFont);
        for(int i = 0; i < textes.size(); i++)
        {
            g.drawString(textes.get(i), stiftx+schriftOffset, stifty+6+schriftOffset + (g.getFontMetrics().getHeight()+2) * (i));
        }
        g.setFont(f);
    }
     private void breakLongStringInLines(FontMetrics FM){

        textes = new ArrayList<String>();
        textes.add(text);
        int W = FM.stringWidth(textes.get(textes.size()-1));
        int H = FM.getHeight()+zeilenabstand;
        while(W > width - schriftOffset * 2)
        {
            int oversize =  W -   width + schriftOffset * 2;
            int skipped  = 0;
            for(int i = textes.get(textes.size()-1).length(); i > 0; i--){
                String sub = textes.get(textes.size()-1).substring(i);
                if(oversize < FM.stringWidth(sub)){
                    boolean broken = false;
                    if(skipped == 14)
                    {
                        i+=12;
                        sub = textes.get(textes.size()-1).substring(i);
                        broken = true;
                    }
                    else if(textes.get(textes.size()-1).charAt(i-1)!=' '){
                        skipped++;
                        continue;
                    }
                    if(broken)
                        textes.set(textes.size()-1,textes.get(textes.size()-1).substring(0,i)+"-");
                    else
                        textes.set(textes.size()-1,textes.get(textes.size()-1).substring(0,i));
                    textes.add(sub);
                    break;
                }
            }
            W = FM.stringWidth(textes.get(textes.size()-1));
        }
        if((H-5)*textes.size()+(schriftOffset-1)*2 < height)
        {
            inLinesBroken = true;
        }

    }
    
    public  void updatePosition(){
    }
}
